package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.clue.AddClueDTO;
import com.qk.dto.clue.QueryPageClueDTO;
import com.qk.dto.clue.QueryPoolClueDTO;
import com.qk.dto.clue.TrackClueDTO;
import com.qk.management.service.ClueService;
import com.qk.vo.clue.QueryClueVO;
import com.qk.vo.clue.QueryPageClueVO;
import com.qk.vo.clue.QueryPoolClueVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author:gzc
 * @Date: 2025/10/06 21:50
 * @Description:
 */
@RestController
@RequestMapping("/clues")
@Slf4j
public class ClueController {
    @Autowired
    private ClueService clueService;

    @GetMapping
    public Result queryListByPage(QueryPageClueDTO clueQueryListByPageDto) {
        log.info("查询线索信息条件:{}", clueQueryListByPageDto);
        PageResult<QueryPageClueVO> queryListByPageVO = clueService.queryListByPage(clueQueryListByPageDto);
        return Result.success(queryListByPageVO);
    }


    @PostMapping
    public Result addClue(@RequestBody AddClueDTO addClueDTO) {
        log.info("添加线索信息:{}", addClueDTO);
        clueService.addClue(addClueDTO);
        return Result.success();
    }


    @PutMapping("/assign/{clueId}/{userId}")
    public Result assignClue(@PathVariable Integer clueId, @PathVariable Integer userId) {
        log.info("分配线索: 线索ID={}, 用户ID={}", clueId, userId);
        clueService.assignClue(clueId, userId);
        return Result.success();
    }


    @PutMapping("/false/{id}")
    public Result convertToFalse(@PathVariable Integer id, @RequestBody Map<String, Object> requestMap) {
        log.info("伪线索id:{}", id);
        Integer reason = (Integer) requestMap.get("reason");
        String remark = (String) requestMap.get("remark");
        log.info("伪处理原因:{}", reason);
        log.info("伪处理备注:{}", remark);
        clueService.convertToFalse(id, reason, remark);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result queryClueById(@PathVariable Integer id) {
        log.info("根据查询线索信息,id:{}", id);
        QueryClueVO clue = clueService.queryClueById(id);
        return Result.success(clue);
    }


    @PutMapping
    public Result trackClue(@RequestBody TrackClueDTO clue) {
        log.info("线索跟进:{}", clue);
        clueService.trackClue(clue);
        return Result.success();
    }


    @PutMapping("/toBusiness/{id}")
    public Result convertToBusiness(@PathVariable Integer id) {
        log.info("将线索转为商机, id: {}", id);
        clueService.convertToBusiness(id);
        return Result.success();
    }


    @GetMapping("/pool")
    public Result queryCluePool(QueryPoolClueDTO queryPoolClueDTO) {
        log.info("查询线索池信息条件:{}", queryPoolClueDTO);
        PageResult<QueryPoolClueVO> queryListByPageVo = clueService.queryCluePool(queryPoolClueDTO);
        return Result.success(queryListByPageVo);
    }
}
